home *** CD-ROM | disk | FTP | other *** search
- Path: keats.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Help : Problems with Arrays of Arrays
- Date: 8 Mar 1996 14:09:57 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4hqb7lINNg6i@keats.ugrad.cs.ubc.ca>
- References: <4houo4$o7@niaomi.iscm.ulst.ac.uk>
- NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
-
- In article <4houo4$o7@niaomi.iscm.ulst.ac.uk>,
- Shaunna McClintock <shauna@iscm.ulst.ac.uk> wrote:
- >
- >Hi,
- >
- >I'm currently writing a program with a structure as follows:
- >
- >Pop is a structure composing of an int and an array of Chrom
- >Chrom is a structure composing of an int and an array of Gene
- >Gene is a structure
- >
- >I've problems in that if in Chrom.. Array of Gene > [3] and Array of
- >Chrom > [30]...the program hangs.. i really need much larger arrays...
- >i'm currently using borlandC on my PC.
-
- Also, consider using dynamic data structures. An array can be replaced by a
- pointer to suitable storage that can syntactically act as an array thanks to
- the way arrays and pointers behave in C.
-
- For example, in chrom, instead of an array, you can declare a pointer to a
- a Gene structure. And in the Gene structure, you can declare a pointer to a
- Pop structure. You then use the malloc() routine to allocate enough space. For
- example, if I declare p to be a pointer to a struct Pop, I can allocate storage
- for 50 such contiguous structures and assign it to p, using:
-
- #include <stdlib.h>
-
- /* declaration of struct Pop */
-
- int main(void)
- {
- struct Pop *p = malloc(50 * sizeof(struct Pop));
-
- return 0;
- }
-
- For the purpose of accessing the 50 structures, you can treat p as though it
- were an array (but keeping in mind that it is not an array, but a pointer to
- storage). Thus you could do
-
- p[1].whatever_member = some_value;
-
- as you would with an array.
-
- --
-
-